package com.bodekeji.dati.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bodekeji.dati.service.DatiService;

@RestController
public class DatiController {

	@Resource
	private DatiService datiService;
	
	@RequestMapping(value="/hello")
	public String hello() {
		return "Hello Dati!";
	}
	
	@RequestMapping(value="/get_sjtm")
	public String getSjtm(int n) {
		JSONArray jsonArray = new JSONArray();
		int num = n==0?10:n;
		List<Map<String, Object>> list = datiService.findSj(num);
		Iterator<Map<String, Object>> listIterator = list.iterator();
		while(listIterator.hasNext()) {
			Map<String, Object> map = listIterator.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", map.get("id"));
			jsonObject.put("fenlei", map.get("fenlei"));
			jsonObject.put("tigan", map.get("tigan"));
			jsonObject.put("xuanxiang1", map.get("xuanxiang1"));
			jsonObject.put("xuanxiang2", map.get("xuanxiang2"));
			jsonObject.put("xuanxiang3", map.get("xuanxiang3"));
			jsonObject.put("xuanxiang4", map.get("xuanxiang4"));
			jsonObject.put("daan", map.get("daan"));
			jsonArray.add(jsonObject);
		}
		return jsonArray.toJSONString();
	}
	
	@RequestMapping(value="/insert_datilishi")
	public String insertDatilishi(String openid,int defen) {
		return "插入数据"+datiService.insertDatilishi(openid, defen)+"条";
	}
	
	@RequestMapping(value="/get_datilishi")
	public String getDatilishi(String openid) {
		JSONArray jsonArray = new JSONArray();
		List<Map<String, Object>> list = datiService.findDatilishiByOpenid(openid);
		Iterator<Map<String, Object>> listIterator = list.iterator();
		while(listIterator.hasNext()) {
			Map<String, Object> map = listIterator.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", map.get("id"));
			jsonObject.put("openid", map.get("openid"));
			jsonObject.put("shijian", map.get("shijian"));
			jsonObject.put("defen", map.get("defen"));
			jsonArray.add(jsonObject);
		}
		return jsonArray.toJSONString();
	}
	
}
